package base

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/remote/gfyx_user_service_remote"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	tenantPb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/profit"
	"git.myscrm.cn/golang/stark/v4"
)

type profitServer struct {
	profitService       profit.ProfitServiceIface
	gfyxUserServiceRepo gfyx_user_service_remote.GfyxUserServiceRepositoryIface
}

func NewProfitServer(
	profitService profit.ProfitServiceIface,
	gfyxUserServiceRepo gfyx_user_service_remote.GfyxUserServiceRepositoryIface,
) basePb.ProfitServiceServer {
	return &profitServer{
		profitService:       profitService,
		gfyxUserServiceRepo: gfyxUserServiceRepo,
	}
}

func (s *profitServer) GetTenantProfitByName(ctx context.Context, request *basePb.GetTenantProfitByNameRequest) (*basePb.GetTenantProfitByNameResponse, error) {
	resp := &basePb.GetTenantProfitByNameResponse{}
	if request.CompanyName == "" {
		return nil, errors.New("公司名称不能为空")
	}
	tenantInfo, err := s.gfyxUserServiceRepo.GetTenantByName(ctx, &tenantPb.GetTenantByNameRequest{Name: request.GetCompanyName()})
	if err != nil {
		return nil, err
	}
	if tenantInfo == nil {
		return resp, nil
	}

	ctx = context.WithValue(ctx, authorize.TENANT_CODE_KEY, tenantInfo.Code)
	list, err := s.profitService.GetTenantProfit(ctx)
	if err != nil {
		return nil, err
	}

	for _, profitPo := range list {
		resp.Profits = append(resp.Profits, &basePb.TenantProfit{
			ProductCode: profitPo,
		})
	}

	return resp, nil
}

// GetTenantProfit 获取租户权益
func (s *profitServer) GetTenantProfit(ctx context.Context, request *basePb.GetTenantProfitRequest) (*basePb.GetTenantProfitResponse, error) {
	resp := basePb.GetTenantProfitResponse{}
	list, err := s.profitService.GetTenantProfit(ctx)
	if err != nil {
		return nil, err
	}

	for _, profitPo := range list {
		resp.Profits = append(resp.Profits, &basePb.TenantProfit{
			ProductCode: profitPo,
		})
	}

	return &resp, nil
}

// GetProfitProvince 获取（省份）数据权限
func (s *profitServer) GetProfitProvince(ctx context.Context, request *basePb.GetProfitProvinceRequest) (*basePb.GetProfitProvinceResponse, error) {
	resp := basePb.GetProfitProvinceResponse{}
	productCodeList, err := s.profitService.GetTenantProfitList(ctx)
	if err != nil {
		return nil, err
	}
	for _, profitPo := range productCodeList {
		resp.Profits = append(resp.Profits, &basePb.TenantProfit{
			ProductCode: profitPo.ProfitCode,
			Number:      int64(profitPo.Number),
		})
	}

	provinceCodeList, err := s.profitService.GetProfitProvince(ctx)
	if err != nil {
		return nil, err
	}
	resp.ProvinceCodeList = provinceCodeList

	return &resp, nil
}

// SaveProfitProvince 设置（省份）数据权限
func (s *profitServer) SaveProfitProvince(ctx context.Context, request *basePb.SaveProfitProvinceRequest) (*basePb.SaveProfitProvinceResponse, error) {
	//if len(request.GetProvinceCode()) == 0 {
	//	return nil, errors.New("省份不能为空")
	//}

	if err := s.profitService.SaveProfitProvince(ctx, request.GetProvinceCode()); err != nil {
		return nil, err
	}
	return &basePb.SaveProfitProvinceResponse{IsSuccess: true}, nil
}

// SaveTenantProfit 保存租户权益
func (s *profitServer) SaveTenantProfit(ctx context.Context, request *basePb.SaveTenantProfitRequest) (*basePb.SaveTenantProfitResponse, error) {
	if len(request.ProfitList) == 0 {
		return &basePb.SaveTenantProfitResponse{}, nil
	}
	resp, err := s.profitService.SaveTenantProfit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc SaveTenantProfit request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil

}
